<template>
  <el-dialog :visible.sync="localVisible" title="新增区域" @close="closeDialog">
    <el-form :model="form" ref="form" label-width="120px" :rules="rules">
      <el-form-item label="所属省份" prop="province">
        <el-input v-model="form.province"></el-input>
      </el-form-item>
      <el-form-item label="区域名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="电话区号" prop="areaCode">
        <el-input v-model="form.areaCode"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import axios from 'axios'
export default {
  name: 'RegionDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      localVisible: this.visible,
      form: {
        province: '',
        name: '',
        areacode: ''
      },
      rules: {
        province: [
          { required: true, message: '请输入所属省份', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ],
        areaCode: [
          { required: true, message: '请输入电话区号', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(newVal) {
      this.localVisible = newVal;
    },
    localVisible(newVal) {
      this.$emit('update:visible', newVal);
    },

  },
  methods: {
    closeDialog() {
      this.localVisible = false;
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const exists = this.checkIfExists().then(result => {
            
            // 在这里访问提取出来的布尔值
            if (result) {
              this.$message.error('该区域已经存在');
            } else {
              axios.post('/api/area/areaadd', this.form)
              this.$message({
                message: '新增成功',
                type: 'success'
              });
              this.$emit('refresh'); // 刷新区域数据
              this.closeDialog();
            }
            // 可以在这里继续处理提取到的布尔值
          })
            .catch(error => {
              console.error('Error:', error);
              // 在出错的情况下进行错误处理
            });;


        } else {
          this.$message.error('请填写所有必填项');
        }
      });
    },
    checkIfExists() {
      // 检查数据是否已经存在
      // 示例逻辑，实际应该从后台接口获取数据进行校验
      let e = false;
      return new Promise((resolve, reject) => {
        axios.post('/api/area/search', this.form)
          .then(res => {
            console.log(res.data);
            if (res.data.success === true) {
              e = true;
            } else {
              e = false;
            }
            resolve(e); // 返回最终结果
          })
          .catch(error => {
            console.error('Error:', error);
            reject(error); // 处理错误情况
          });
      });
    }
  }
};
</script>

<style scoped>
/* 样式 */
</style>